/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.dominatingspecies.frontend.element;

import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.dao.DataAccessException;
import org.springframework.security.access.prepost.PreAuthorize;
import pa165.dominating.species.tos.ElementTo;
import pa165.dominatingspecies.service.impl.ElementServiceImpl;

/**
 *
 * @author Ondrej Prazak
 */
@PreAuthorize("hasRole('ADMIN')")
public class DeleteElementLink extends Link<Void> {

    @SpringBean(name = "elementService")
    private ElementServiceImpl elementService;
    private final ElementTo ele;

    public DeleteElementLink(String componentId, ElementTo elem) {
        super(componentId);
        ele = elem;
    }

    @Override
    public void onClick() {
        boolean thrown = false;
        try {
            elementService.deleteElement(ele);
        } catch (DataAccessException ex) {
            thrown = true;
            getSession().warn(new StringResourceModel("removeFail", this, null, new Object[]{ele.getId()}).getString());
            setResponsePage(ElementsPage.class);
        }
        if (!thrown) {
            getSession().info(new StringResourceModel("removeSucc", this, null, new Object[]{ele.getId()}).getString());
            setResponsePage(ElementsPage.class);
        }
    }
}
